package com.app.util;

import java.awt.Color;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.PropertySet;
import org.apache.poi.hpsf.PropertySetFactory;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.DocumentInputStream;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.textmining.text.extraction.WordExtractor;

import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.rtf.RtfWriter2;


@SuppressWarnings({"unused"})
public class WordUtil {

	/**
	 * 读world文件 返回数据
	 * 
	 * @param String
	 *            filepath 文件路径
	 * @return String data
	 * @throws IOException
	 * @throws Exception
	 */
	public static String readWord(String filepath) throws Exception {
		FileInputStream in = new FileInputStream(filepath);
		WordExtractor extractor = new WordExtractor();
		String str = extractor.extractText(in);
		return str;
	}



	// 写入文件
	public static void writeFile(String pdfpath) throws Exception {
		// Document document = new Document();
		/*
		 * document.addTitle("安全基线检查结果统计报表"); // 标题
		 * document.addSubject("统计报表");// 主题 document.addAuthor("chenc"); // 作者
		 * document.addCreator("chenc"); // 创建人
		 */
		// 生成名为 HelloWorld.pdf 的文档
		// PdfWriter.getInstance(document, new FileOutputStream(pdfpath));
		Document document = new Document(PageSize.A4, 20, 20, 20, 20);// 上下左右的页面边距
		FileOutputStream buffer = new FileOutputStream(pdfpath);
		RtfWriter2 writer = RtfWriter2.getInstance(document, buffer);// 设置输出流

		// writer.setEncryption(RtfWriter2.STRENGTH128BITS, "", "",
		// RtfWriter2.AllowCopy | PdfWriter.AllowPrinting);
		// 标题字体风格
		Font font = ITextFontUtil.getCnBoldFont(18, Color.BLACK); // 设置字体
		HeaderFooter header = new HeaderFooter(
				new Phrase("安全基线检查结果统计报表", font), false);
		// false表示，没有含有数字（页码）
		header.setAlignment(Element.ALIGN_CENTER);// 居中
		document.setHeader(header);
		// 打开文档，将要写入内容
		BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
				"UniGB-UCS2-H", false);
		Font fontChinese = new Font(bfChinese, 9, Font.NORMAL, Color.BLACK);
		Font fontChinese2 = new Font(bfChinese, 9, Font.NORMAL, Color.RED);
		document.open();
		document.add(new Paragraph("资产（10.10.69.65）- Oracle结果：", fontChinese2));
		// document.newPage();//换页

		// 设置 Table
		Table table = new Table(2, 13);
		int width[] = { 10, 20 };
		table.setWidths(width);
		table.setWidth(50); // 占页面宽度 80%
		table.setBorderWidth(0); // 边框宽度
		table.setBorderColor(Color.BLACK); // 边框color
		table.setPadding(2); // 单元格内边距
		table.setSpacing(0); // 单元格外边距
		table.setAlignment(Element.ALIGN_LEFT); // table对齐方式
		table.setAutoFillEmptyCells(true);// 没数据的cell为空

		/*
		 * cell.setVerticalAlignment(Element.ALIGN_TOP);
		 * cell.setRowspan(3);//独占三行 table.addCell(cell);
		 */
		Cell c1 = new Cell(new Phrase("任务名称", fontChinese));
		c1.setBackgroundColor(Color.GRAY);
		table.addCell(c1);
		table.addCell(new Cell("#2"));
		c1 = new Cell(new Phrase("任务描述", fontChinese));
		c1.setBackgroundColor(Color.GRAY);
		table.addCell(c1);
		table.addCell(new Cell("#4"));
		c1 = new Cell(new Phrase("创建人", fontChinese));
		c1.setBackgroundColor(Color.GRAY);
		table.addCell(c1);
		table.addCell(new Cell("#6"));
		c1 = new Cell(new Phrase("所属省份", fontChinese));
		c1.setBackgroundColor(Color.GRAY);
		table.addCell(c1);
		table.addCell(new Cell("#12"));
		c1 = new Cell(new Phrase("所属城市", fontChinese));
		c1.setBackgroundColor(Color.GRAY);
		table.addCell(c1);
		table.addCell(new Cell("#14"));
		c1 = new Cell(new Phrase("所属部门", fontChinese));
		c1.setBackgroundColor(Color.GRAY);
		table.addCell(c1);
		table.addCell(new Cell("#16"));
		c1 = new Cell(new Phrase("执行时间", fontChinese));
		c1.setBackgroundColor(Color.GRAY);
		table.addCell(c1);

		table.addCell(new Cell("#21"));
		c1 = new Cell(new Phrase("资产组名称", fontChinese));
		c1.setBackgroundColor(Color.GRAY);
		table.addCell(c1);
		table.addCell(new Cell("#23"));
		c1 = new Cell(new Phrase("资产名称", fontChinese));
		c1.setBackgroundColor(Color.GRAY);
		table.addCell(c1);
		table.addCell(new Cell("#25"));
		c1 = new Cell(new Phrase("资产得分", fontChinese));
		c1.setBackgroundColor(Color.GRAY);
		table.addCell(c1);
		table.addCell(new Cell("#31"));
		c1 = new Cell(new Phrase("自定义检查项", fontChinese));
		c1.setBackgroundColor(Color.GRAY);
		table.addCell(c1);
		table.addCell(new Cell("#33"));
		c1 = new Cell(new Phrase("检查项名称", fontChinese));
		c1.setBackgroundColor(Color.GRAY);
		table.addCell(c1);
		table.addCell(new Cell("#35"));
		c1 = new Cell(new Phrase("检查项得分", fontChinese));
		c1.setBackgroundColor(Color.GRAY);
		table.addCell(c1);
		table.addCell(new Cell("#37"));
		document.add(table);
		document.add(new Paragraph("图表：", fontChinese));

		// // 可以是绝对路径，也可以是URL
		// Image img = Image.getInstance("E:/资料/tb.jpg");
		// img.setAlignment(Image.ALIGN_LEFT);
		// img.scalePercent(80);//这只图片缩放比例，这里是80%
		// //img.setAbsolutePosition(0, 0);
		// document.add(img);

		document.add(new Paragraph("检查子项：", fontChinese));

		Table table2 = new Table(7);
		int width2[] = { 26, 26, 26, 20, 20, 50, 50 };
		table2.setWidths(width2);
		table2.setWidth(98); // 占页面宽度 80%
		table2.setBorderWidth(0); // 边框宽度
		table2.setBorderColor(Color.BLACK); // 边框color
		table2.setPadding(2); // 单元格内边距
		table2.setSpacing(0); // 单元格外边距
		table2.setAlignment(Element.ALIGN_LEFT); // table对齐方式
		table2.setAutoFillEmptyCells(true);// 没数据的cell为空

		Cell c2 = new Cell(new Phrase("检查子项分类", fontChinese));
		c2.setBackgroundColor(Color.GRAY);
		table2.addCell(c2);
		c2 = new Cell(new Phrase("检查子项编号", fontChinese));
		c2.setBackgroundColor(Color.GRAY);
		table2.addCell(c2);
		c2 = new Cell(new Phrase("检查子项名称", fontChinese));
		c2.setBackgroundColor(Color.GRAY);
		table2.addCell(c2);
		c2 = new Cell(new Phrase("重要程度", fontChinese));
		c2.setBackgroundColor(Color.GRAY);
		table2.addCell(c2);
		c2 = new Cell(new Phrase("检查结果", fontChinese));
		c2.setBackgroundColor(Color.GRAY);
		table2.addCell(c2);
		c2 = new Cell(new Phrase("检查结果提示", fontChinese));
		c2.setBackgroundColor(Color.GRAY);
		table2.addCell(c2);
		c2 = new Cell(new Phrase("操作建议", fontChinese));
		c2.setBackgroundColor(Color.GRAY);
		table2.addCell(c2);
		for (int i = 1; i <= 21; i++) {
			Cell cell2 = new Cell(new Phrase("#" + i, fontChinese));
			table2.addCell(cell2);
		}
		document.add(table2);

		document.close();
	}

	public static void writeTaskWord(OutputStream os, List tabs) {

	}

	public static void writeTaskWord_en(OutputStream os, List tabs) {

	}

	public static void main(String[] args) {
		try {
			 System.out.println(readWord("E:/应用/美元指数历史走势分析.doc"));
			// writeWordFile("d://test.doc", "<br>asdasd");
			//writeFile("d://test.doc");
			System.out.println("world util finished");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
